package com.eric.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import com.eric.domain.dto.MaterialOutDto;
import com.eric.service.IMaterialOutService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 * 出库表单 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/material-out")
public class MaterialOutController {
    private final IMaterialOutService materialOutService;

    //    添加出库表单
    @PostMapping("/saveOne")
    public SaResult saveOne(MaterialOutDto materialOutDto, MultipartFile[]photoFiles, HttpServletRequest request) {

        return materialOutService.saveOneService(materialOutDto,photoFiles,request);
    }

//    修改一条表单
    @PutMapping("/modifyOne")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    @SaCheckPermission("sys:modify")
    public SaResult modifyOne(MaterialOutDto materialOutDto,MultipartFile[]photoFiles, HttpServletRequest request,String[] photoList) {

        return materialOutService.modifyOneService(materialOutDto,photoFiles,request,photoList);
    }

//    根据id删除一条

    @DeleteMapping("/{id}")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult deleteOne(@PathVariable("id") Integer id){
        return materialOutService.deleteOneByIdService(id);
    }

    // todo 作废   根据多个id批量删除
    @DeleteMapping("/deleteByIds")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult deleteByIds(@RequestBody ArrayList<Integer> ids){
        return materialOutService.deleteByIdsService(ids);
    }

    //    分页查询所有数据
    @GetMapping("/getAllByPage")
    public SaResult getAllByPage(Integer current,Integer pageSize){
        return materialOutService.getAllByPage(current,pageSize);
    }
//    分页查询所有数据的条数
    @GetMapping("/getAllCounts")
    public SaResult getAllCounts(){
        return materialOutService.getAllCountsService();
    }


    //    根据Id查询一条数据
    @GetMapping("/{id}")
    public SaResult getOneById(@PathVariable("id") Integer id){
        return materialOutService.getOneByIdService(id);
    }

    //    根据两个时间段查询
    @GetMapping("/getByBetweenTimes")
    public SaResult getAllByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end, Integer current, Integer pageSize){
        return materialOutService.getByBetweenTimesService(start,end,current,pageSize);
    }

    //    根据创建时间查询
    @GetMapping("/getByCreateTime")
    public SaResult getByCreateTime(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date createTime,Integer current,Integer pageSize){
        return materialOutService.getByCreateTimeService(createTime,current,pageSize);
    }

    //    根据修改时间查询
    @GetMapping("/getByUpdateTime")
    public SaResult getByUpdateTime(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date updateTime,Integer current,Integer pageSize){
        return materialOutService.getByUpdateTimeService(updateTime,current,pageSize);
    }

    @GetMapping("/getByMaterialName")
    public SaResult getByMaterialName(String materialName,Integer current,Integer pageSize){
        return materialOutService.getByMaterialNameService(materialName,current,pageSize);
    }

    @GetMapping("/getByLastUpdateBy")
    public SaResult getByLastUpdateBy(String lastByName,Integer current,Integer pageSize){
        return materialOutService.getByLastUpdateByService(lastByName,current,pageSize);
    }

    @GetMapping("/getByCreateBy")
    public SaResult getByCreateBy(String createByName,Integer current,Integer pageSize){
        return materialOutService.getByCreateByService(createByName,current,pageSize);
    }

    @GetMapping("/getAllByMoon")
    public SaResult getAllByMoon(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date moonDate, Integer current,Integer pageSize){
        return materialOutService.getAllByMoon(moonDate,current,pageSize);
    }

    //   好好想想  根据材料子类型并根据Number数量排序查询，
    @GetMapping("/getByTypeName")
    public SaResult getByTypeName(String parentType,String childTypeName,Integer orderBy,Integer current,Integer pageSize){
        return materialOutService.getByTypeName(parentType,childTypeName,orderBy,current,pageSize);
    }

//   //  多功能查询接口，这里面的name包括设备名、创建人，最后修改人，项目名称
    @GetMapping("/getAllByMultifunctionName")
    public SaResult getAllByMultifunctionName(String name,Integer current,Integer pageSize){
        return materialOutService.getAllByMultifunctionNameService(name,current,pageSize);
    }

    // 配合多功能查询接口，获取到所有条数
    @GetMapping("/getAllCountsByMultifunctionName")
    public SaResult getAllCountsByMultifunctionName(String name,Integer current,Integer pageSize){
        return materialOutService.getAllCountsByMultifunctionNameService(name,current,pageSize);
    }

    @GetMapping("/getMaterialOutByBetweenTimes")
    public SaResult getByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end, Integer current, Integer pageSize){
        return materialOutService.getByBetweenTimesService(start,end,current,pageSize);
    }
    @GetMapping("/getAllMaterialOutCountsByBetweenTimess")
    public SaResult getAllCountsByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){
        return materialOutService.getAllCountsByBetweenTimes(start,end);
    }


    @GetMapping("/getMaterialOutListByMaterialId")
    public SaResult getOneMaterialOutList(boolean timeStatus, Integer materialId,Integer current,Integer pageSize,String projectName,String recipient,@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){

        return materialOutService.getMaterialOutListByMaterialId(timeStatus,materialId,current,pageSize,projectName,recipient,start,end);
    }

    @GetMapping("/getMaterialOutListByMaterialIdCounts")
    public SaResult getOneMaterialOutListCount(boolean timeStatus,Integer materialId,String projectName,String recipient,@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){
        return  materialOutService.getMaterialOutListCountByMaterialId(timeStatus,materialId,projectName,recipient,start,end);
    }

}
